//
//  MovieDetailsViewController.m
//  UI_豆瓣
//
//  Created by WangZe on 15/10/13.
//  Copyright © 2015年 王泽龙. All rights reserved.
//

#import "MovieDetailsViewController.h"
#import "MovieList.h"
#import "UIImageView+WebCache.h"
#import "SaveTool.h"
@interface MovieDetailsViewController ()<UIAlertViewDelegate>
@property (nonatomic, retain)UIScrollView *myScrollView;
//@property (nonatomic, retain)MBProgressHUD *hud;
@property (nonatomic, retain)NSMutableArray *movieArr;
@end

@implementation MovieDetailsViewController

- (void)creatView
{
    UILabel *movieNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 20)];
    self.navigationItem.titleView = movieNameLabel;
    [movieNameLabel release];
    // 创建imageView
    UIImageView *movieImageView = [[UIImageView alloc] initWithFrame:CGRectMake(30, 50, 120, 200)];
    [self.myScrollView addSubview:movieImageView];
    [movieImageView release];
    //
    UILabel *ratingLabel = [[UILabel alloc] initWithFrame:CGRectMake(190, 55, 90, 38)];
    [self.myScrollView addSubview:ratingLabel];
    [ratingLabel release];
    //
    
    UILabel *countLabel = [[UILabel alloc] initWithFrame:CGRectMake(260, 55, 80, 38)];
    [self.myScrollView addSubview:countLabel];
    [countLabel release];
    //
    UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(190, 55 + 38 , 90, 38)];
    [self.myScrollView addSubview:timeLabel];
    [timeLabel release];
    
    //
    UILabel *runtimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(190, 55 + 38 * 2, 90, 38)];
    [self.myScrollView addSubview:runtimeLabel];
    [runtimeLabel release];
    
    //
    UILabel *genresLabel = [[UILabel alloc] initWithFrame:CGRectMake(190, 55 + 38 * 3, 120, 38)];
    [self.myScrollView addSubview:genresLabel];
    [genresLabel release];
    
    //
    UILabel *adressLabel = [[UILabel alloc] initWithFrame:CGRectMake(190, 55 + 38 * 4, 90, 38)];
    [self.myScrollView addSubview:adressLabel];
    [adressLabel release];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(30, 250, 200, 50)];
    label.text = @"制作人";
    label.font = [UIFont systemFontOfSize:25];
    [self.myScrollView addSubview:label];
    UILabel *actor = [[UILabel alloc] initWithFrame:CGRectMake(30, 270, 315, 100)];
    [self.myScrollView addSubview:actor];
    actor.numberOfLines = 0;
    [actor release];
    
    
    
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(30, 360, 200, 100)];
    [self.myScrollView addSubview:label1];
    label1.text = @"电影情节";
    label1.font = [UIFont systemFontOfSize:25];
    [label1 release];
    
    UILabel *plot_simple = [[UILabel alloc] initWithFrame:CGRectMake(30, 440, 315, 300)];
    [self.myScrollView addSubview:plot_simple];
    plot_simple.numberOfLines = 0;
    [plot_simple release];
    
    
    
    
    
    // 创建一个url
    NSString *path = [NSString stringWithFormat:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/searchmovie.php?movieId=%@", self.movieID];
    NSURL *url = [NSURL URLWithString:path];
    // 创建一个请求
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    
    // 建立连接
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        MovieList *movie = [[MovieList alloc] init];
        [movie setValuesForKeysWithDictionary:dic[@"result"]];
        movieNameLabel.text = movie.title;
        [movieImageView sd_setImageWithURL:[NSURL URLWithString:movie.poster]];
        ratingLabel.text = [NSString stringWithFormat:@"评分: %@", movie.rating];
        countLabel.text = [NSString stringWithFormat:@"(%@评论)", movie.rating_count];
        timeLabel.text = movie.release_date;
        runtimeLabel.text = movie.runtime;
        genresLabel.text = movie.genres;
        adressLabel.text = movie.country;
        plot_simple.text = movie.plot_simple;
        actor.text = movie.actors;
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.myScrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
    self.myScrollView.contentSize = CGSizeMake(0, self.view.frame.size.height * 2 - 500);
    [self.view addSubview:self.myScrollView];
    [_myScrollView release];
    
    [self creatView];
   
    if ([SaveTool isHaveMovieInPlist:self.tempMovie]) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"收藏" style:UIBarButtonItemStylePlain target:self action:@selector(collectAction:)];
    }
    else
    {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"已收藏" style:UIBarButtonItemStylePlain target:self action:@selector(collectAction:)];
    }
    
    
}









- (void)collectAction:(UIBarButtonItem *)barButtonItem
{
    
    if ([barButtonItem.title isEqualToString:@"已收藏"]) {
    UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"已收藏" message:@"已收藏,不需要重复收藏" delegate:self cancelButtonTitle:@"返回" otherButtonTitles:@"删除收藏",nil] autorelease];
            [alert show];
        
    }
    else
    {
        [SaveTool saveMovieToPlist:self.tempMovie];
        barButtonItem.title = @"已收藏";
    }
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == 1)
    {
       [SaveTool cancleMovieInPlist:self.tempMovie];
        self.navigationItem.rightBarButtonItem.title = @"收藏";
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
